const playerModel = require('../models/players.js')

class PlayerCtrl {

    async list() {
        let lst = await playerModel.find();
        return lst.map(m=>m.toJSON());
    }

    async create(playerData) {
        let P = new playerModel(playerData);
        const a = await P.save();
        return a.toJSON();
    }

    async retrieve(id) {
        let P = await playerModel.findById(id);
        return P?P.toJSON(): null;
    }

    async upsert(id, playerData) {
        playerData.id = id;
        let P = await playerModel.upsert(id, playerData);
        return P.toJSON();
    }
    async removeById(id) {
        await playerModel.removeById(id);
        return 1;
    }
    async find(filter)
    {
        let r = await playerModel.find(filter);
        return r.map(e=>e.toJSON());
    }
}

module.exports = new PlayerCtrl();